﻿@{
    ViewBag.Title = "UserList";
    Layout = "~/Views/Shared/_LayoutChild.cshtml";
}
@using Agro.Web;
@section head {
    @if (false) {<script src="~/Scripts/jquery-1.6.4-vsdoc.js" type="text/javascript"></script>}
    <link href="@Url.Content("~/Content/zTreeStyle/zTreeStyle.css")" rel="stylesheet" type="text/css" />
    <script src="@Url.Content("~/Scripts/jquery.ztree.all-3.0.min.js")" type="text/javascript"></script>
    @Html.Partial("_JqGrid")
    <script language="javascript" type="text/javascript">
        var selectId = "";
        var gridId = "#tableId";
        var curSelectNode = null;
        var Userid = "";
        $(document).ready(function () {
            loadBaseDicTree('/SystemManage/Organization/GetTreeNode/', 'organization_id', 'organization_parentid', 'organization_name', true);
            funGridInit();
        });
        function zTreeOnClick(event, treeId, treeNode, clickFlag) {
            curSelectNode = treeNode;
            selectId = curSelectNode.organization_id;
            _zTreeObj.expandNode(treeNode, true); //展开点击节点
            Search();
        }
        function zTreeOnAsyncSuccess(event, treeId, treeNode, msg) {
            //当treeNode为空时，就让treeNode等于当前选中的第一个节点，如果当前的选中节点也为空，那就默认选中第一个节点。

            if (treeNode != null) {
                curSelectNode = treeNode;
                selectId = treeNode.organization_id;
            }

            if (curSelectNode == null) {//如果加载完，没有选中节点，则默认选中第一个节点
                var nodes = _zTreeObj.getNodes();
                if (nodes.length > 0) {
                    _zTreeObj.selectNode(nodes[0]);
                    curSelectNode = nodes[0];
                    selectId = nodes[0].organization_id;
                }
            }
            zTreeOnClick(event, treeId, curSelectNode, 1);
        }
        function DeleteUser() {
            $.funListDelete(gridId, "User_ID", '@Url.Content("~/SystemManage/User/Delete_User")');
        }

        function setUserMenus() {
            var id = jqGridHelper.getSelRowData(gridId, "User_ID");
            u = "/SystemManage/User/UserMenu/" + id;
            if (id != null) {
                parent.asyncbox.open({
                    url: u,
                    data: '',
                    title: "用户-菜单权限",
                    width: 500,
                    height: 450,
                    scroll: 'no',
                    modal: true,
                    btnsbar: parent.$.btn.OK, //按钮栏配置请参考 “辅助函数” 中的 $.btn。
                    callback: function (action) {
                        var t = this;
                        if (action == 'ok') {
//                            DialogFrame($(this).attr("id")).Save(t);
//                            return false;
                        }
                        if (action == 'cancel') {

                        }
                    }
                });
            }
            else {
                alert('没有选中的数据');
            }
        }

        function setUserActions() {
            var id = jqGridHelper.getSelRowData(gridId, "User_ID");
            u = "/SystemManage/User/UserAction/" + id;
            if (id != null) {
                parent.asyncbox.open({
                    url: u,
                    data: '',
                    title: "用户-操作权限",
                    width: 500,
                    height: 450,
                    scroll: 'no',
                    modal: true,
                    btnsbar: parent.$.btn.OK, //按钮栏配置请参考 “辅助函数” 中的 $.btn。
                    callback: function (action) {
                        var t = this;
                        if (action == 'ok') {
//                            DialogFrame($(this).attr("id")).Save(t);
//                            return false;
                        }
                        if (action == 'cancel') {

                        }
                    }
                });
            }
            else {
                alert('没有选中的数据');
            }
        }

        function editUser(type) {
            if (type == "add") {
                if (selectId == "") {
                    alert("请选择单位!");
                    return;
                }
            }
            //判断不能添加同级用户
            if ('@ViewBag.IsAdmin' != '1' && '@ViewBag.CurrUserOrg' == selectId) {
                parent.asyncbox.alert('不能添加或编辑同级用户!', '提示');
                return;
            }

            //根据type的参数 进行设置
            if (selectId == null || selectId == "") return; //如果没有选择单位，则返回
            var u = "/SystemManage/User/UserEdit?id=&organizationid=" + selectId;
            if (type == "edit")
                u = "/SystemManage/User/UserEdit?id=" + Userid + "&organizationid=" + selectId;

            parent.asyncbox.open({
                url: u,
                data: '',
                title: type == "add" ? "新增" : "编辑",
                width: 520,
                height: 350,
                scroll: 'no',
                modal: true,
                btnsbar: parent.$.btn.OKCANCEL, //按钮栏配置请参考 “辅助函数” 中的 $.btn。
                callback: function (action) {
                    var t = this;
                    if (action == 'ok') {
                        DialogFrame($(this).attr("id")).Save(t);
                        return false;
                    }
                    if (action == 'cancel') {

                    }
                    if (action == 'close') {

                    }
                }
            });
        }
        function Search() {
            //查询
            $(gridId).setGridParam({ page: 1 });
            $(gridId).trigger("reloadGrid");
        }
        function funGridInit() {
            //初始化表格
            var option = {
                height: $("#divGridContainer").height() - 51,
                width: $("#divGridContainer").width() - 2,
                url: '@Url.Content("~/SystemManage/User/GetUserList")',
                autowidth: false,
                datatype: "json",
                sortname: 'User_Code',
                sortorder: 'asc',
                colModel: [
                { label: '用户编号', name: 'User_Code', width: 100 },
                { label: '用户名称', name: 'User_Name', width: 120 },
                { label: '性别', name: 'User_Sex', width: 60, align: 'center' },
                { label: '所属单位', name: 'Organization_Name', width: 120 },
                { label: '电话', name: 'User_Telephone', width: 100 },
                { label: '手机', name: 'User_Phone', width: 100 },
                { label: '说明', name: 'User_Remark', width: 120 },
                { hidden: true, name: 'User_ID' },
                { hidden: true, name: 'Organization_ID' },
   	            ],
                onSelectRow: function (rowid, cellname, value, iRow, iCol) {
                    Userid = $(gridId).getCell(rowid, "User_ID");
                    selectId = $(gridId).getCell(rowid, "Organization_ID");
                },
                ondblClickRow: function (rowid) {
                    if (rowid >= 0) {
                        editUser('edit');
                        selectId = $(gridId).getCell(rowid, "Organization_ID");
                    }
                },
                beforeRequest: function (d) {
                    if (curSelectNode == null) return false;
                    var data = { organizeId: curSelectNode == null ? '' : curSelectNode.organization_id };
                    $(gridId).jqGrid('setGridParam', { postData: data });
                }
            };
            var o = jqGridHelper.getConfig("#pageId", option);
            $(gridId).jqGrid(o);
        }
    </script>
}
<!--头部导航,不用时可以去掉-->
<div class="ui-layout-north">
    <div id="headertitle">
    </div>
    <div class="buttonlist" style="float: right; height: 34px; line-height: 36px; margin-right: 20px;">
        @Html.ButtonExt("菜单权限", "group_key.png", "User_Menus", "setUserMenus();")
        @Html.ButtonExt("操作权限", "group_key.png", "User_Actions", "setUserActions();")
        @Html.ButtonExt("新增", "add.png", "User_Add", "editUser('add');")
        @Html.ButtonExt("编辑", "edit.png", "User_Add", "editUser('edit');")
        @Html.ButtonExt("删除", "delete.png", "User_Del", "DeleteUser();")

        <!--    <a onclick="Search()" class="button green"><span class="icon-botton"
                style="background: url('/Content/16/search.png') no-repeat scroll 0px 4px;"></span>
                查询</a>
         -->
    </div>
</div>
<!--左侧导航，不用时直接去掉-->
<div class="ui-layout-west">
    @Html.Partial("_BaseDicTree")
</div>
<!--主窗体，页面都需要的div-->
<div id="divGridContainer" class="ui-layout-center" style="overflow: auto;">
    <table id="tableId">
    </table>
    <div id="pageId">
    </div>
</div>
